package com.li.shop.controller;

import com.li.shop.domain.*;
import com.li.shop.mapper.ShopcardMapper;
import com.li.shop.service.ProductDetailsService;
import com.li.shop.service.ProductPicService;
import com.li.shop.service.ProductService;
import com.li.shop.service.ShopCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/shopcard")
public class ShopCardController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ShopCardService shopCardService;

    @Autowired
    private ProductDetailsService productDetailsService;

    @Autowired
    private ProductPicService productPicService;

    @Autowired
    private ShopcardMapper shopcardMapper;

    /**
     * 将商品信息保存到购物车中并且显示购物车中商品的数量，判断如果为同一件商品
     * 则只需要加上商品的数量，如果不是同一种商品则新建该商品
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(Model model, @RequestParam("id") String id, @RequestParam("num") String num, HttpSession session) {
        System.out.println("当前商品编号pid为" + id + "得到商品的总共的数量为" + num);
        Product product = productService.selectByPrimaryKey(id);
        //得到商品的单价
        Long dazhe=product.getDazhe();
        model.addAttribute("product", product);
        //得到当前session中的u_id
        String s = session.getAttribute("id").toString();
        Shopcard shopcard=new Shopcard();
        String sid=shopcard.getsId();

        //保存商品信息到购物车中:如果当前商品存在则进行合并操作，如果不存在则进行添加操作
        ShopcardExample example=new ShopcardExample();
        example.createCriteria().andPIdEqualTo(id).andUIdEqualTo(s);
        List<Shopcard> shopcardList=shopcardMapper.selectByExample(example);

        //添加操作
        if(shopcardList.isEmpty())
        {
            shopcard.setsId(sid);
            shopcard.setuId(s);
            shopcard.setpId(id);
            shopcard.setPnum(num);
            shopcard.setJine(Integer.parseInt(num)*Integer.parseInt(String.valueOf(dazhe)));
            shopCardService.insert(shopcard);
        }
        else {
            shopcard=shopCardService.s(id);
            shopcard.getPnum();
            System.out.println("原始数量为"+shopcard.getPnum());
            int a=Integer.parseInt(num);
            int b=Integer.parseInt(shopcard.getPnum());
            int sum=a+b;
            shopcard.setPnum(String.valueOf(sum));
            shopcard.setJine(sum*Integer.parseInt(String.valueOf(dazhe)));

            shopcardMapper.updateByExampleSelective(shopcard,example);
        }

        //查出商品的数量
        ShopcardExample shopcardExample = new ShopcardExample();
        shopcardExample.createCriteria().andUIdEqualTo(s);
        Long num1 = shopCardService.countByExample(shopcardExample);

        //查出商品的详情
        ProductDetails productDetails=productDetailsService.find(id);
        ProductPic productPic=productPicService.find(id);
        model.addAttribute("product1",productDetails);
        model.addAttribute("productPic",productPic);
        model.addAttribute("sid",sid);

        session.setAttribute("num", num1);
        return "/before/introduction";
    }

    /**
     * 根据id删除购物车中的商品信息
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(Model model, @RequestParam("id") String id) {
        System.out.println("得到的用户商品的id为" + id);
        shopCardService.update(id);
        return "redirect:/product/listall";
    }
    @RequestMapping (value = "/deletecart",method=RequestMethod.GET)//wang删除购物车中商品
    public String deleteCartShop(@RequestParam("id") String id, HttpSession session) {
        int deleted = shopCardService.deleteCartShopByPrimaryKey(id);
        System.out.println("我的id是："+id);
        User user=(User) session.getAttribute("session_user");
        return "redirect:/product/listall";

    }


}
